﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using CodeGeneratorLibrary;
using CodeGeneratorLibrary.DAL;
using System.Linq;
using Options = CodeGeneratorLibrary.Options;

namespace BitrixCodeGenerator.Controllers
{
	public class OptionsController : Controller
	{
		public ActionResult Edit()
		{
			return View(Options.Factory);
		}

		[HttpPost]
		public ActionResult Edit(Options options)
		{
			var o = Options.Factory;
			o.ConnectionString = options.ConnectionString;
			o.AuthorName = options.AuthorName;
			o.SqlOperation = options.SqlOperation;
			o.TableToGenerate = options.TableToGenerate;

			o.Save();
			return View(Options.Factory);
		}

		public ActionResult Word(string word)
		{
			word = word.Trim().ToLower();

			var wordAlias = en.GrammWordAlias.SingleOrDefault(x => x.Alias == word);
			if (wordAlias == null)
			{
				wordAlias = new GrammWordAlias {Alias = word};
				en.GrammWordAlias.AddObject(wordAlias);
				en.SaveChanges();
			}

			wordAlias = en.GrammWordAlias.Single(x => x.Alias == word);

			ViewBag.NumberGroup = en.GrammNumberGroup.OrderBy(x => x.Id).ToList();
			ViewBag.Declension = en.GrammDeclension.OrderBy(x => x.Id).ToList();

			ViewBag.Alias = wordAlias.Alias;

			IEnumerable<GrammWord> grammWord = wordAlias.GrammNominativeWord != null
				? (IEnumerable<GrammWord>)wordAlias.GrammNominativeWord.GrammWords
				: new List<GrammWord>();

			return View(grammWord);
		}

		[HttpPost]
		public ActionResult Word()
		{
			string alias = Request.Form["alias"].ToLower().Trim();
			var wordAlias = en.GrammWordAlias.Single(x => x.Alias == alias);


			Dictionary<int, Dictionary<int, string>> nums = new Dictionary<int, Dictionary<int, string>>();

			foreach (var key in Request.Form.AllKeys)
			{
				var ks = key.Split('_');
				int numId;
				int decId;
				if (ks.Length == 2 && int.TryParse(ks[0], out numId) && int.TryParse(ks[1], out decId))
				{
					var value = Request.Form[key].Trim().ToLower();
					if (!nums.ContainsKey(numId))
						nums.Add(numId, new Dictionary<int, string>());

					var decs = nums[numId];
					decs.Add(decId, value);
				}
			}

			var nominativeWord = string.Empty;

			if (nums.ContainsKey(WordDeclention.SingularGrammNumberGroupId)
			    && nums[WordDeclention.SingularGrammNumberGroupId].ContainsKey(WordDeclention.NominativeGrammDeclensionId)
			    && !string.IsNullOrWhiteSpace(nums[WordDeclention.SingularGrammNumberGroupId][WordDeclention.NominativeGrammDeclensionId]))
			{
				nominativeWord = nums[WordDeclention.SingularGrammNumberGroupId][WordDeclention.NominativeGrammDeclensionId];

				GrammWord nominativeWordDeclension = wordAlias.GrammNominativeWord;/*en
						.GrammWord
						.SingleOrDefault(x => x.Word == nominativeWord
							&& x.GrammNumberGroup.Id == SingularGrammNumberGroupId
							&& x.GrammDeclension.Id == NominativeGrammDeclensionId)*/
				;


				if (nominativeWordDeclension == null)
				{
					nominativeWordDeclension = new GrammWord()
					                           	{
					                           		Word = nominativeWord,
					                           		DeclensionId = WordDeclention.NominativeGrammDeclensionId,
					                           		NumberGroupId = WordDeclention.SingularGrammNumberGroupId
					                           	};

					en.GrammWord.AddObject(nominativeWordDeclension);
					en.SaveChanges();

					nominativeWordDeclension.GrammNominativeWord = nominativeWordDeclension;
					wordAlias.GrammNominativeWord = nominativeWordDeclension;
					en.SaveChanges();
				}

				List<GrammWord> grammWords = nominativeWordDeclension.GrammWords.ToList();

				foreach (var num in nums)
				{
					var numPair = num;
					foreach (var dec in nums[numPair.Key])
					{
						var decPair = dec;

						var grWord = grammWords
							.SingleOrDefault(x => x.NumberGroupId == numPair.Key
								&& x.DeclensionId == decPair.Key);

						if (grWord != null)
						{
							grWord.Word = dec.Value;
						}
						else
						{
							var wordDeclension = new GrammWord()
							                     	{
							                     		Word = dec.Value,
							                     		DeclensionId = decPair.Key,
							                     		NumberGroupId = numPair.Key,
																			GrammNominativeWord = nominativeWordDeclension
							                     	};
							en.GrammWord.AddObject(wordDeclension);
						}
					}
				}
				en.SaveChanges();
			}
			return RedirectToAction("Word", new { word = alias });
		}



		CodeGeneratorEntities en = new CodeGeneratorEntities(); 

		protected override void Dispose(bool disposing)
		{
			//en.Dispose();
		}
	}
}